#ifndef BE_MODEL_SYSTEM_H_INCLUDED
#define BE_MODEL_SYSTEM_H_INCLUDED

#include <SDL/SDL.h>
#include <GL/glew.h>
#include <vector>

#include "src/utils/texturestore.h"
#include "src/common/be_obj_loader.h"
#include "src/utils/displaylists.h"

#include "src/graphics/be_graphics_system.h"
#include "src/graphics/be_buffer.h"

#include "src/filesystem/be_filesystem.h"

#include <boost/scoped_ptr.hpp>
#include <boost/scoped_array.hpp>

struct Model
{

	Model( const std::string& filename ) :
		m_filename(filename),
		m_isUploaded(false),
		m_arrayBuffer(0),
		m_elementArrayBuffer(0),
		m_vertexArray(0)
	{ }
	
	~Model() {}

		const std::string&	getFilename() const { return m_filename; }
		
		void setNumberVertices( unsigned int numberVertices ) { m_numberVertices = numberVertices; }
		unsigned int		getNumberVertices() const { return m_numberVertices; }

		void setNumberNormals( unsigned int numberNormals ) { m_numberNormals = numberNormals; }
		unsigned int		getNumberNormals() const { return m_numberNormals; }

		void setNumberTexCoor( unsigned int numberTexCoor ) { m_numberTexCoor = numberTexCoor; }
		unsigned int		getNumberTexCoor() const { return m_numberTexCoor; }

		void setNumberVIndices( unsigned int numberVIndices ) { m_numberVIndices = numberVIndices; }
		unsigned int		getNumberVIndices() const { return m_numberVIndices; }

		bool			isUploaded() const { return m_isUploaded; }

		boost::scoped_array<btScalar> vertices;
		boost::scoped_array<float> normals;
		boost::scoped_array<float> texcoor;
		boost::scoped_array<int> indices; 

		map<string, Material> matlist;
		vector<Facematerial> facematerials;

		virtual void		draw( btScalar* drawposition );
		void			draw();
		void			upload();

	private:
		std::string	m_filename;
		bool		m_isUploaded;

		unsigned int m_numberVertices;
		unsigned int m_numberNormals;
		unsigned int m_numberTexCoor;
		unsigned int m_numberVIndices;
		
		boost::scoped_ptr<BeArrayBuffer>	m_arrayBuffer;
		boost::scoped_ptr<BeElementArrayBuffer>	m_elementArrayBuffer;
		boost::scoped_ptr<BeVertexArray>	m_vertexArray;

		typedef map <string, Material>::const_iterator matlist_iterator;
		matlist_iterator matit;

		void loadMaterialfile( const string& mpath, const string& file );

		BeGraphicsSystem m_graphicsSystem;
};

class BeModelSystem
{
	public:
		static BeModelSystem*	Instance();
		~BeModelSystem();

		boost::shared_ptr<Model> load( const std::string& filename , const btVector3& scale); // FIXME pass string
		void emptyStore();
	protected:
		BeModelSystem();
		static BeModelSystem*	_instance;

		std::vector<boost::shared_ptr<Model> > models;
	private:
		
};

#endif
